package com.czr.vo;

import cn.hutool.http.HttpStatus;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.czr.constant.WgConstant;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
 * 统一响应消息报文
 *
 * @param <T> 　T对象
 * @author pangu
 */
@Data
@Builder
@AllArgsConstructor
@Accessors(chain = true)
@ApiModel(value = "统一响应消息报文")
public class Result<T> implements Serializable {

	private static final long serialVersionUID = 1L;

	@ApiModelProperty(value = "状态码", required = true)
	private int code;

	@ApiModelProperty(value = "消息内容", required = true)
	private String msg;

	@ApiModelProperty(value = "时间戳", required = true)
	private long time;

	@ApiModelProperty(value = "业务数据")
	@JsonInclude(JsonInclude.Include.NON_NULL)
	private T data;

	private Result() {
		this.time = System.currentTimeMillis();
	}


	private static <T> Result<T>  Result(int code, T data, String msg) {
		Result<T> apiResult = new Result<>();
		apiResult.setCode(code);
		apiResult.setData(data);
		apiResult.setMsg(msg);
		return apiResult;
	}

	/**
	 * 返回状态码
	 *
	 * @param <T>        泛型标识
	 * @return ApiResult
	 */
	public static <T> Result<T> success(int code) {
		return Result(code,null,WgConstant.DEFAULT_SUCCESS_MESSAGE);
	}

	public static <T> Result<T> success(String msg) {
		return Result(HttpStatus.HTTP_OK,null, msg);
	}

	public static <T> Result<T> success(int code, String msg) {
		return  Result(code,null, msg);
	}

	public static <T> Result<T> data(T data) {
		return data(data, WgConstant.DEFAULT_SUCCESS_MESSAGE);
	}

	public static <T> Result<T> data(T data, String msg) {
		return data(HttpStatus.HTTP_OK, data, msg);
	}

	public static <T> Result<T> data(int code, T data, String msg) {
		return Result(code, data, data == null ? WgConstant.DEFAULT_NULL_MESSAGE : msg);
	}

	public static <T> Result<T> fail() {
		return Result(HttpStatus.HTTP_BAD_REQUEST,null, "");
	}

	public static <T> Result<T> fail(String msg) {
		return Result(HttpStatus.HTTP_BAD_REQUEST,null, msg);
	}

	public static <T> Result<T> fail(int code, String msg) {
		return Result(code,null, msg);
	}

	public static <T> Result<T> fail(int code) {
		return Result(code,null, "");
	}

}
